package com.wanmait.demo.controller.manageController;

import com.wanmait.demo.pojo.Admin;
import com.wanmait.demo.service.AdminService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class AdminLoginController {
    @Resource
    private AdminService adminService;

    @RequestMapping("adminlogin1")
    public String adminlogin(String err)
    {
//        if(err!=null)
//        {
//            return "/manage/login?err=1";
//        }
        return "/manage/login";
    }

    //闫应鹏
    @RequestMapping("/adminlogin")
    public String login(String userName, String passWord,  HttpSession session)
    {
        boolean rememberMe=false;
        try {
        UsernamePasswordToken token = new UsernamePasswordToken(userName,passWord);

        //主体
        Subject subject= SecurityUtils.getSubject();

        //主体进行登录

            token.setRememberMe(rememberMe);

            subject.login(token);
            Admin admin = (Admin)subject.getPrincipal();
            session.setAttribute("admin",admin);
            return "/manage/index";
        } catch (AuthenticationException e) {
            return "/manage/login?err=用户名或密码错误";
        }
    }

}
